package com.gnerv.author.freemarker.tag;

import com.gnerv.author.entity.Com;
import com.gnerv.author.freemarker.FreemarkerComponent;
import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapperBuilder;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Gnerv LiGen
 * @description
 * @data 2019/4/3
 */
@FreemarkerComponent("g_coms")
public class ComsTagDirective implements TemplateDirectiveModel {

    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
        List<Com> coms = new ArrayList<>();

        Com com = new Com();
        com.setName("000");
        List<Com> zi = new ArrayList<>();
        zi.add(com);
        zi.add(com);
        zi.add(com);

        Com com1 = new Com();
        com1.setName("111");
        com1.setZi(zi);

        Com com2 = new Com();
        com2.setName("222");
        com2.setZi(zi);

        Com com3 = new Com();
        com3.setName("333");
        com3.setZi(zi);

        coms.add(com1);
        coms.add(com2);
        coms.add(com3);

        TemplateModel tm = builder.build().wrap(coms);
        environment.setVariable("coms", tm);
        templateDirectiveBody.render(environment.getOut());
    }

}
